/**
 *
 * User: Zhikang.Peng
 * Date: 2018/06/20 11:24
 */
package references

import (
	"gitee.com/thghh/jvmgo/instructions/base"
	"gitee.com/thghh/jvmgo/rtda"
	"gitee.com/thghh/jvmgo/rtda/heap"
)

// 创建一个引用型（如类，接口， 数组）的数组，并将其引用值压入栈顶。
type ANEW_ARRAY struct {
	base.Index16Instruction
}

func (self *ANEW_ARRAY) Execute(frame *rtda.Frame) {
	cp := frame.Method().Class().ConstantPool()
	classRef := cp.GetConstant(self.Index).(*heap.ClassRef)
	componentClass := classRef.ResolvedClass()
	stack := frame.OperandStack()
	count := stack.PopInt()
	if count < 0 {
		panic("java.lang.NegativeArraySizeException")
	}
	arrayClass := componentClass.ArrayClass()
	array := arrayClass.NewArray(uint(count))
	stack.PushReference(array)
}
